package com.dhwl.hsh.merchant.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ListView;

import com.dhwl.hsh.lib.bean.resp.CarTypeResp;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.MvpView;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.merchant.presenter.TruckPresenter;
import com.dhwl.hsh.merchant.ui.adapter.TruckListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 选择车型
 *
 * @author Derek.Kidd
 * @time 2016/4/26 18:32
 */
public class ChooseTruckActivity extends BaseActivity implements MvpView{

    @Bind(R.id.list_truck)
    ListView listTruck;

    TruckListAdapter listAdapter;

    List<CarTypeResp> carTypeResp;


    TruckPresenter truckPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_truck_list;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setTitleContent("选择车型");
        topbarLeftImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onComplete();
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        onComplete();
    }

    @Override
    protected void initData() {
        truckPresenter = new TruckPresenter();
        truckPresenter.attachView(this);
        truckPresenter.getTruck();

        carTypeResp = new ArrayList<CarTypeResp>();
        listAdapter = new TruckListAdapter(this,carTypeResp);
        listTruck.setAdapter(listAdapter);
        listAdapter.notifyDataSetChanged();

    }

    private void onComplete() {
        Intent intent = new Intent();
        if(listAdapter.getCount()<=0){
            setResult(RESULT_CANCELED, intent);
        }else {
            intent.putExtra(Constant.Order.KEY_TRUCK_TYPE, (Parcelable) listAdapter.getItem(listAdapter.getCheckNum()));
            setResult(RESULT_OK, intent);
        }
    }

    @Override
    public void onFailure(HshException e) {

    }

    @Override
    public void onSuccess(Object object) {
        List<CarTypeResp> carTypeResp = (List<CarTypeResp>) object;
        listAdapter.addAll(carTypeResp);

    }
}
